//
//  UserCenterHomeTVCCollectionViewCell.m
//  much
//
//  Created by Believer Just on 2019/1/8.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "UserCenterHomeTVCCollectionViewCell.h"

@interface UserCenterHomeTVCCollectionViewCell ()

@property (nonatomic, strong) UIImageView *imageView; // 图标
@property (nonatomic, strong) UILabel *titleLabel; // 文字
@property (nonatomic, strong) UILabel *messageLabel; // 是否有名片
@property (nonatomic, strong) UILabel *countLabel; // 数量

@end

@implementation UserCenterHomeTVCCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

/** 初始化视图 */
- (void)initView {
    
}

- (void)setCollectionCellData:(NSDictionary *)dataDic {
    [self.imageView setImage:IMAGE_NAME(dataDic[CellImageName])];
    self.titleLabel.text = dataDic[CellTitle];
    
    if ([dataDic[CellTag] integerValue] == UCHTVCTypeMarketingBusinessCard) {
        self.messageLabel.hidden =! [dataDic[CellHasMessage] boolValue];
    } else {
        self.messageLabel.hidden = YES;
    }
    
    [self showCount:dataDic[CellCount]];
}
- (void)showCount:(NSString *)count {
    if ([count isEqualToString:@"0"]) {
        self.countLabel.hidden = YES;
    } else {
        self.countLabel.hidden = NO;
        if ([count integerValue] >= 10 && [count integerValue] <= 99) {
            self.countLabel.cornerRadius = 5.5;
            self.countLabel.text = [NSString stringWithFormat:@"%@", count];
            [self.countLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.imageView).with.offset(- 5.5);
                make.height.mas_equalTo(11);
                make.width.mas_equalTo(16);
                make.right.equalTo(self.imageView).with.offset(10);
            }];
        } else if ([count integerValue] >99) {
            self.countLabel.cornerRadius = 5.5;
            self.countLabel.text = [NSString stringWithFormat:@"%@+", @"99"];
            [self.countLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.imageView).with.offset(- 5.5);
                make.height.mas_equalTo(11);
                make.width.mas_equalTo(22);
                make.right.equalTo(self.imageView).with.offset(10);
            }];
        } else {
            self.countLabel.cornerRadius = 6.5;
            self.countLabel.text = [NSString stringWithFormat:@"%@", count];
            [self.countLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.imageView).with.offset(- 6.5);
                make.right.equalTo(self.imageView).with.offset(7);
                make.width.mas_equalTo(13);
                make.height.mas_equalTo(13);
            }];
        }
    }
}


#pragma mark - 懒加载
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [UIImageView new];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_centerY).with.offset(-1);
            make.centerX.equalTo(self.contentView);
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(20);
        }];
    }
    return _imageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = Color80;
        _titleLabel.font = APPFONT(12);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageView.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.imageView);
            make.left.equalTo(self.contentView).with.offset(4);
            make.right.equalTo(self.contentView).with.offset(- 4);
        }];
    }
    return _titleLabel;
}
- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [UILabel new];
        _messageLabel.layer.masksToBounds = YES;
        _messageLabel.layer.cornerRadius = 5;
        _messageLabel.backgroundColor = Color175030;
        [self.imageView addSubview:_messageLabel];
        [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.imageView.mas_top);
            make.centerX.mas_equalTo(self.imageView.mas_right);
            make.width.and.height.mas_equalTo(10);
        }];
    }
    return _messageLabel;
}
- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [UILabel new];
        CGFloat w = 12;
        _countLabel.cornerRadius = w / 2.0;
        _countLabel.backgroundColor = Color175030;
        _countLabel.textColor = [UIColor whiteColor];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.font = APPFONT(10);
        _countLabel.adjustsFontSizeToFitWidth = YES;
        [self.contentView insertSubview:_countLabel aboveSubview:self.imageView];
        [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.imageView).with.offset(w /2.0);
            make.top.equalTo(self.imageView).with.offset(-w /2.0);
            make.width.and.height.mas_equalTo(w);
        }];
    }
    return _countLabel;
}


#pragma mark - public methods
+ (NSString *)cellClass {
    return NSStringFromClass(self);
}

@end
